﻿TUYENPHAT = function () {
    
}
TUYENPHAT.MaTuyen = -1;
TUYENPHAT.columns = new Array("Mã Tuyến Phát", "Tên Tuyến Phát");
TUYENPHAT.fieldText = new Array("MaTuyenPhat", "TenTuyenPhat");
TUYENPHAT.arrays = new Array();
TUYENPHAT.isInsert = 0;
var numberPage = 1;
TUYENPHAT.Init = function () {
    TUYENPHAT.arrays = new Array();
    var obj;
    obj = new CheckField("TenTuyenPhat", 1, "ErrTenTuyenPhat");
    TUYENPHAT.Add(obj);    
}

TUYENPHAT.Add = function (obj) {
    if (TUYENPHAT.arrays.contains(obj) == false) {
        TUYENPHAT.arrays.push(obj);//khi them 1 doi tuong vao mang
    }
}

TUYENPHAT.Insert = function () {
    var serviceUrl = Constants.service + "insertTblTuyenPhats";
    if (Constants.OnSubmit(TUYENPHAT.arrays) == false) {
        return false;
    }
    var data = {
        MaTuyenPhat: $("#MaTuyenPhat").val(),
        TenTuyenPhat: $("#TenTuyenPhat").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {            
            TUYENPHAT.GetData();
            TUYENPHAT.CanCel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}


TUYENPHAT.Delete = function (id) {    
    var data = {
        MaTuyenPhat : id
    };
    var json = JSON.stringify({ item: data });
    a = confirm(Constants.messDelete);
    if (a == false) {
        return;
    }
    var serviceUrl = Constants.service + "deleteTblTuyenPhats";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (data) {
            TUYENPHAT.GetData();           
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TUYENPHAT.UpdateItem = function () {
    var serviceUrl = Constants.service + "updateTblTuyenPhats";
    if (Constants.OnSubmit(TUYENPHAT.arrays) == false) {
        return false;
    }
    var data = {
        MaTuyenPhat: $("#MaTuyenPhat").val(),
        TenTuyenPhat: $("#TenTuyenPhat").val(),
        DaXoa: false
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {            
            TUYENPHAT.GetData();
            TUYENPHAT.CanCel();
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TUYENPHAT.CreateTable = function (r, nbpage, data) {
    var myTable = TUYENPHAT.TitleColumns(TUYENPHAT.columns);   
    myTable += TUYENPHAT.DataColumnsOftr(data, r, nbpage);
    myTable += "</table>";
    $("#div_list").children().remove();
    $("#div_list").append(myTable);
    $("#div_content").show();
}

TUYENPHAT.GetData = function () {    
    var serviceUrl = Constants.service + "getAllTblTuyenPhats";
    var data = { url: window.location.host };
    var json = JSON.stringify({ item: data });
    TUYENPHAT.MaTuyen = -1;
    TUYENPHAT.isInsert = 0;
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var results = msg.getAllTblTuyenPhatsResult;
            var dataTables = results;
            TUYENPHAT.CreateTable(Constants.rows, numberPage, dataTables);
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TUYENPHAT.TitleColumns = function (_columns) {
    var myColumns = "<table class=\"rounded-corner\">";
    myColumns += "<thead><tr>";
    for (var i = 0; i < _columns.length; i++) {
        myColumns += "<th class=\"rounded\" scope=\"col\">" + _columns[i] + "</th>";
    }
    myColumns += "<th class=\"rounded\" scope=\"col\">Edit</th>";
    myColumns += "<th class=\"rounded-q4\" scope=\"col\">Delete</th>";
    myColumns += "</tr></thead>";
    var n = _columns.length + 1;
    myColumns += "<tfoot><tr><td class=\"rounded-foot-left\" colspan=\"" + n + "\"><em> </em></td>";
    myColumns += "<td class=\"rounded-foot-right\">&nbsp;</td>";
    myColumns += "</tr></tfoot>";
    return myColumns;
}

TUYENPHAT.DataColumnsOftr = function (data, rs, _numberPage) {
    var myDataColumns = "<tbody>";
    var n = data.length;
    var j = (_numberPage - 1) * rs;
    var r = rs;    
    for (var i = j; i < r && i < n; i++) {
        var id = data[i].MaTuyenPhat;
        myDataColumns += "<tr onclick='return TUYENPHAT.OnClick("+ id + ")' id=\"" + id + "\">"; 
        myDataColumns += "<td>" + data[i].MaTuyenPhat + "</td>"
        myDataColumns += "<td>" + data[i].TenTuyenPhat + "</td>"
        myDataColumns += "<td><img onclick='return TUYENPHAT.EditItem("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/user_edit.png\"></td>";
        myDataColumns += "<td><a class=\"ask\" ><img onclick='return TUYENPHAT.Delete("+ id +")' border=\"0\" title=\"\" alt=\"" + id + "\" src=\"images/trash.png\"></a></td>";
    }
    myDataColumns += "</tbody>";
    return myDataColumns;
}
//TUYENPHAT.MaTuyen
TUYENPHAT.OnClick = function (id) {
    TUYENPHAT.MaTuyen = id;
    $(".active").removeClass();
    $("#" + id).addClass("active");
    console.log("TUYENPHAT.MaTuyen : " + TUYENPHAT.MaTuyen);
}

TUYENPHAT.OnClickEdit = function () {
    if (TUYENPHAT.MaTuyen == -1) {
        alert(Constants.selectEdit);
    }
    else
        TUYENPHAT.EditItem(TUYENPHAT.MaTuyen);
}

TUYENPHAT.OnClickDelete = function () {
    if (TUYENPHAT.MaTuyen == -1) {
        alert(Constants.selectDelete);
    }
    else
        TUYENPHAT.Delete(TUYENPHAT.MaTuyen);
}

TUYENPHAT.EditItem = function (id) {
    var temp_item = "Modules/tphat/Edit";
  
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Sửa" });
            TUYENPHAT.GetItem(id);
        }
    });
}

TUYENPHAT.CanCel = function () {
    $("#dialog").empty();
    $("#dialog").dialog("close");
}

TUYENPHAT.GetItem = function (id) {

    var serviceUrl = Constants.service + "getItemByIdTblTuyenPhat";
    var data = { MaTuyenPhat: id };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getItemByIdTblTuyenPhatResult;
            if (result != null && result != undefined) {
                $("#MaTuyenPhat").val(result.MaTuyenPhat);
                $("#TenTuyenPhat").val(result.TenTuyenPhat);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TUYENPHAT.Clear = function () {
    Constants.clearAll(TUYENPHAT.fieldText);
}

TUYENPHAT.AddNewItem = function () {
    var temp_item = "Modules/tphat/AddNew";
    TUYENPHAT.isInsert = 1;
    $("#dialog").empty();
    $.ajax({
        url: temp_item + ".htm",
        success: function (data) {
            $('#dialog').append(data);
            $("#dialog").dialog("open");
            $("#dialog").dialog({ title: "Thêm Mới" });
            TUYENPHAT.getMa();
        }
    });
    return false;
}

TUYENPHAT.Enter = function () {
    $("#MaTuyenPhat").keypress(function (event) {
        if (event.keyCode == 13) {
            $("#TenTuyenPhat").focus();
            event.preventDefault();
        }
    });
    $("#TenTuyenPhat").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            if (TUYENPHAT.isInsert == 1) {
                TUYENPHAT.Insert();
            }
            else {
                TUYENPHAT.UpdateItem();
            }
        }
    });
}

TUYENPHAT.getMa = function () {
    var serviceUrl = Constants.service + "getMaTblTuyenPhat";
    $.ajax({
        type: "Post",
        url: serviceUrl, // Location of the service
        //data: json, //Data sent to server
        contentType: "application/json", // content type sent to server
        dataType: "json", //Expected data format from server
        cache: false,
        success: function (msg) {
            var result = msg.getMaTblTuyenPhatResult;
            if (result != undefined && result != null) {
                $("#MaTuyenPhat").val(result);
            }
        },
        error: function (err) {
            // When Service call fails
        }
    });
}

TUYENPHAT.Search = function () {
    var serviceUrl = Constants.service + "getFindTblTuyenPhats";
    var data = {
        value: $("#search").val()
    };
    var json = JSON.stringify({ item: data });
    $.ajax({
        type: "Post",
        url: serviceUrl,
        data: json,
        contentType: "application/json",
        dataType: "json",
        cache: false,
        success: function (msg) {
            var results = msg.getFindTblTuyenPhatsResult;
            if (results != null && results.length > 0) {
                TUYENPHAT.CreateTable(Constants.rows, numberPage, results);
            }
            else {
                $("#div_list").children().remove();
                $("#div_list").html(Constants.find);
            }
        }
    });
}

$(document).ready(function () {
    TUYENPHAT.GetData();
    TUYENPHAT.Init();
    
    $("#search").keypress(function (event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            TUYENPHAT.Search();
        }
    });
    $("#search_on").click(function (event) {
        event.preventDefault();
        TUYENPHAT.Search();
    });

    $("#edit").click(function () {
        var temp_item = "Modules/tphat/AddNew.htm";
        $("#dialog").empty();
        $.ajax({
            url: temp_item,
            success: function (data) {
                $('#dialog').append(data);
                $("#dialog").dialog("open");
                $("#dialog").dialog({ title: "Them moi" });
                TUYENPHAT.getMa();
            }
        });
        return false;
    });
});
